<template>
  <div id="cesiumContainer"></div>
</template>

<script setup>
import * as Cesium from 'cesium';
import {onMounted} from "vue";

onMounted(async () => {
  Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI0NTI3OGI2Ni05YmJhLTRlMTYtYjdiNC1hM2E5ZjI1MGYxZmIiLCJpZCI6MTQ0NTcxLCJpYXQiOjE3MDMxNTQ2MzZ9.-ab5sCTIXoyCUMMGv35NQBbR0h7pxsxu78D3AfCVTek'
  const customImageryProvider = new Cesium.ArcGisMapServerImageryProvider({
    url: '//services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer'
  })
  const mapboxImageryProvider = new Cesium.UrlTemplateImageryProvider({
    url: 'https://a.tiles.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94ZGV2dXNlcjUyMCIsImEiOiJjbHFrb2w3M2IyNHB2MnFtZWx1ZzFjNHZmIn0.CMoLsalSeXlJiLBNyUK1nA'
  })
  var url = 'http://localhost:8081/geoserver/nurc/wms'; //Geoserver URL
  // Cesium之Viewer初始常用配置
  const viewer = new Cesium.Viewer('cesiumContainer', {
    shadows: false, // 阴影
    animation: false, // 左下角控制动画
    baseLayerPicker: false, // 右上角图层选择器
    fullscreenButton: false, // 全屏
    geocoder: false, // 右上角搜素
    homeButton: false, // 主页
    infoBox: false, // 信息框
    scene3DOnly: true, // 仅仅显示3D，可以隐藏右上角2D和3D按钮
    selectionIndicator: false, // 绿色选择框
    timeline: false, // 最下面时间轴
    navigationHelpButton: true, // 右上角帮助按钮
    navigationInstructionsInitiallyVisible: true, // 导航指令
    useDefaultRenderLoop: true,
    showRenderLoopErrors: true,
    projectionPicker: false, // 投影选择器
    // terrainProvider: Cesium.createWorldTerrainAsync(), // 地形
    // imageryProvider:  wmsImageryProvider // 影像图
  });
  viewer.scene.globe.depthTestAgainstTerrain = true; // 地形高度
  // 取消双击时间
  viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
  viewer._cesiumWidget._creditContainer.style.display = "none"; // 隐藏cesium icon
  // document.querySelector('.cesium-infoBox-camera').style.display = 'none';//隐藏infobox camera
  // 加载离线地图服务 - web服务影像提供器来实例化影像图层
  const imageryLayers = viewer.imageryLayers;
  const scene = viewer.scene;
  const provider = new Cesium.WebMapServiceImageryProvider({
    url: "http://localhost:8086/geoserver/ne/wms",
    layers: "ne:world",
    parameters: {
      format: "image/png",
      transparent: true,
      srs: "EPSG:4326"
    },
  });
  imageryLayers.addImageryProvider(provider);

  const handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
  // handler.setInputAction(() => {
  //   console.log('监听到左键弹起', )
  // }, Cesium.ScreenSpaceEventType.LEFT_UP);
  // // 删除鼠标左键弹起事件
  // handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_UP)
  // handler.setInputAction(() => {
  //   console.log('鼠标滚动事件', )
  // }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
  handler.setInputAction((e) => {
      console.log('e::: ', e) // eg: {position: {x: 123, y: 8012.123}} [Cartesian2]
      console.log('e::: ', e.position) // { "x": 620, "y": 157.33334350585938}
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK)

  // =》【相机事件】

  // 更新前
   function handler01(){
    console.log('preUpdate - 更新前事件', )
  }
  scene.preUpdate.addEventListener(handler01)
  scene.preUpdate.removeEventListener(handler01)

  // 更新后
  const handler02 = () => {
    console.log('postUpdate - 更新后事件', )
  }
  scene.postUpdate.addEventListener(handler02)
  scene.postUpdate.removeEventListener(handler02)

  // 渲染前
  const handler03 = () => {
    console.log('preRender - 场景渲染前事件', )
  }
  scene.preRender.addEventListener(handler03)
  scene.preRender.removeEventListener(handler03)

  // 渲染后
  const handler04 = () => {
    console.log('postRender - 场景渲染后事件', )
  }
  scene.postRender.addEventListener(handler04)
  scene.postRender.removeEventListener(handler04)
})


</script>

<style>
html,
body,
#cesiumContainer {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
</style>
